package com.ojoin.trade.bpm.web.support;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;

import com.ojoin.trade.bpm.web.domain.Bpm;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.domain.Role;
import com.ojoin.trade.common.web.domain.User;

/**
 * 流程工具
 */
public class BpmUtils {
	 
	/**
	 * 获取流程表单URL
	 * @param formKey
	 * @param act 表单传递参数
	 * @return
	 */
	public static String getFormUrl(String formKey, Bpm act){
		StringBuilder formUrl = new StringBuilder();
		String formServerUrl ="";
		
		formUrl.append(formServerUrl);
		formUrl.append(formKey).append(formUrl.indexOf("?") == -1 ? "?" : "&");
		formUrl.append("bpm.taskId=").append(act.getTaskId() != null ? act.getTaskId() : "");
		formUrl.append("&bpm.taskName=").append(act.getTaskName() != null ? CommonUtils.urlEncode(act.getTaskName()) : "");
		formUrl.append("&bpm.taskDefKey=").append(act.getTaskDefKey() != null ? act.getTaskDefKey() : "");
		formUrl.append("&bpm.procInsId=").append(act.getProcInsId() != null ? act.getProcInsId() : "");
		formUrl.append("&bpm.procDefId=").append(act.getProcDefId() != null ? act.getProcDefId() : "");
		formUrl.append("&bpm.status=").append(act.getStatus() != null ? act.getStatus() : "");
		formUrl.append("&id=").append(act.getBusinessId() != null ? act.getBusinessId() : "");
		
		return formUrl.toString();
	}
	
	/**
	 * 转换流程节点类型为中文说明
	 * @param type 英文名称
	 * @return 翻译后的中文名称
	 */
	public static String parseToZhType(String type) {
		Map<String, String> types = new HashMap<String, String>();
		types.put("userTask", "用户任务");
		types.put("serviceTask", "系统任务");
		types.put("startEvent", "开始节点");
		types.put("endEvent", "结束节点");
		types.put("exclusiveGateway", "条件判断节点(系统自动根据条件处理)");
		types.put("inclusiveGateway", "并行处理任务");
		types.put("callActivity", "子流程");
		return types.get(type) == null ? type : types.get(type);
	}

	public static UserEntity toActivitiUser(User user){
		if (user == null){
			return null;
		}
		UserEntity userEntity = new UserEntity();
		userEntity.setId(user.getLoginName());
		userEntity.setFirstName(user.getName());
		userEntity.setLastName(StringUtils.EMPTY);
		userEntity.setPassword(user.getPassword());
		userEntity.setEmail(user.getEmail());
		userEntity.setRevision(1);
		return userEntity;
	}
	
	public static GroupEntity toActivitiGroup(Role role){
		if (role == null){
			return null;
		}
		GroupEntity groupEntity = new GroupEntity();
		groupEntity.setId(role.getEnname());
		groupEntity.setName(role.getName());
		groupEntity.setType(role.getRoleType());
		groupEntity.setRevision(1);
		return groupEntity;
	}
	
}
